package com.meringer.entities;

import java.io.Serializable;
import java.lang.String;
import java.util.List;

import javax.persistence.*;

/**
 * Entity implementation class for Entity: User
 *
 */
@NamedQueries({
@NamedQuery(name="User.getUsers",
    query="SELECT m FROM User m")          
})
@Entity
@Table(name="T_USERS")

public class User implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	@Id
	@Column(name="UID", nullable=false, length = 30)
	private String username;
        
    @Column(name="PASSWORD", nullable=false, length = 120)
	private String password;
    
    @Column(name="REAL_NAME", nullable=false, length = 60)
	private String realName;
    
    @Column(name="BUDGET", nullable=false)
	private Integer budget;
    
    @Column(name="AGE")
	private Integer age;
    
    @Column(name="IMG_PATH")
	private String imgPath;
    
    @Column(name="IS_ACTIVE")
	private boolean isActive;
    
	@Column(name="EXPERIENCE", nullable=false)
	private Integer experience;
	
    @OneToMany(mappedBy="user", cascade=CascadeType.ALL)
	private List<ChatMessage> chatMessages;

    @OneToOne
    @JoinColumn(name="MAIN_CAR", nullable=true)
    private Car mainCar;
    
    @OneToMany(mappedBy="owner", cascade=CascadeType.ALL)
	private List<Car> cars;
	
	@ManyToMany()
	@JoinTable(name="T_PRIVILEGES",
	     joinColumns=@JoinColumn(name="UID", referencedColumnName="UID"),
	     inverseJoinColumns=@JoinColumn(name="ROLE", referencedColumnName="ROLE"))
	private List<Role> roles;
	
	@ManyToMany
	@JoinTable(name="T_RACE_RESULTS",
	     joinColumns=@JoinColumn(name="UID", referencedColumnName="UID"),
	     inverseJoinColumns=@JoinColumn(name="RACE_ID", referencedColumnName="RACE_ID"))
	private List<Race> races;
	
	@Transient
	private boolean isAdmin;
	
	@Transient
	private boolean isUser;
	
	public User() {
	}	
	
	public User(String username, String password, String realName, Integer budget, 
			Integer age, Integer experience, String imgPath,
			boolean isActive, List<Role> roles) {
		super();
		this.username = username;
		this.password = password;
		this.realName = realName;
		this.budget = budget;
		this.age = age;
		this.experience = experience;
		this.imgPath = imgPath;
		this.isActive = isActive;
		this.roles = roles;
	}

	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}   
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}   
	public Integer getBudget() {
		return this.budget;
	}

	public void setBudget(Integer budget) {
		this.budget = budget;
	}   
	public String getRealName() {
		return this.realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}   
	public Integer getAge() {
		return this.age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}   
	public String getImgPath() {
		return this.imgPath;
	}

	public void setImgPath(String imgPath) {
		this.imgPath = imgPath;
	}   
	public boolean getIsActive() {
		return this.isActive;
	}

	public void setIsActive(boolean isActive) {
		this.isActive = isActive;
	}
   
	public List<Car> getCars() {
		return cars;
	}

	public void setCars(List<Car> cars) {
		this.cars = cars;
	}

	public List<Role> getRoles() {
		return roles;
	}


	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<ChatMessage> getChatMessages() {
		return chatMessages;
	}

	public void setChatMessages(List<ChatMessage> chatMessages) {
		this.chatMessages = chatMessages;
	}

	public Integer getExperience() {
		return experience;
	}

	public void setExperience(Integer experience) {
		this.experience = experience;
	}

	public Car getMainCar() {
		return mainCar;
	}

	public void setMainCar(Car mainCar) {
		this.mainCar = mainCar;
	}

	public List<Race> getRaces() {
		return races;
	}

	public void setRaces(List<Race> races) {
		this.races = races;
	}
	
	public boolean getIsUser() {
		return isUser;
	}

	public void setIsUser(boolean isUser) {
		this.isUser = isUser;
	}
	
	public boolean getIsAdmin() {
		return this.isAdmin;
	}
	
	public void setIsAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if (obj != null && obj instanceof User && ((User)obj).getUsername() != null)
			return ((User)obj).getUsername().equals(this.username);
		return false;
	}
}
